<?php
namespace Admin\Controller;

class PsortController extends BaseController{
    public function index(){
    	$Psort = D('Psort');
    	$this->assign('All',$Psort->all($this->page($Psort->total())));
    	$this->display();
    }
    
    public function add(){
    	if (IS_POST){
    		$Psort = D('Psort');
    		$object = $Psort->insert();
    		if (is_numeric($object)){
    			$object>0 ? $this->success(U(CONTROLLER_NAME.'/index'),'产品分类添加成功！') : $this->failed('产品分类添加失败！');
    		}else{
    			$this->failed($object);
    		}
    	}
    	$this->display();
    }
    
    public function update(){
		if (I('get.id')){
			$Psort = D('Psort');
			if (IS_POST){
				$object = $Psort->update();
				is_numeric($object) ? $this->success(U(CONTROLLER_NAME.'/index'),'产品分类修改成功！') : $this->failed($object);
			}
			$object = $Psort->one();
			if (!$object) $this->failed('不存在此产品分类！');
			$this->assign('One',$object);
			$this->display();
		}else{
			$this->failed('非法操作！');
		}
    }
	
    public function sort(){
    	if (IS_POST){
    		$Psort = D('Psort');
    		foreach (I('post.sort') as $key=>$value){
    			if (!is_numeric($value)) continue;
    			$Psort->sort($key,$value);
    		}
    		$this->success(C('PREV_URL'),'产品分类排序成功！');
    	}
    }
    
    public function delete(){
		if (I('get.id')){
			//$this->failed('演示站，数据无法删除！');
			$Psort = D('Psort');
			if (IS_POST) $Psort->remove() ? $this->success(I('post.prev'),'产品分类删除成功！') : $this->failed('产品分类删除失败！');
			if (!$Psort->one()) $this->failed('不存在此产品分类！');
    		$this->confirm('您真的要删除这条数据么？');
    	}else{
    		$this->failed('非法操作！');
    	}
    }
}